package com.zjp.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;
@Configuration
public class ShiroConfig {
    //ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFacoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        System.out.println("3.getShiroFilterFacoryBean");

        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);

        Map<String, String> filterMap = new LinkedHashMap<>();
        //授权
        filterMap.put("/user/add", "perms[user:add]");//想要访问add需要有add权限
        filterMap.put("/user/update", "perms[user:update]");
//        filterMap.put("/user/add", "authc");
//        filterMap.put("/user/update", "authc");
        bean.setFilterChainDefinitionMap(filterMap);

        //1设置登录请求
        bean.setLoginUrl("/toLogin");//不能省略斜杠
        //2未授权页面
        bean.setUnauthorizedUrl("/unauthorized");

        return bean;
    }
    //DefaultWebSecurityManager
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        //关联userRealm
        manager.setRealm(userRealm);

        return manager;
    }
    //创建realm对象，需要自定义类
    @Bean
    public UserRealm userRealm(){

        return new UserRealm();
    }
    //整合ShiroDialect:用来整合shiro thymeleaf
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}
